package com.example.micro_property_springboot.controller;

import com.example.micro_property_springboot.pojo.SysParam;
import com.example.micro_property_springboot.pojo.vo.SysParamVO;
import com.example.micro_property_springboot.service.SysParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统参数控制器
 * 提供系统参数相关的REST API接口
 */
@RestController
@RequestMapping("/api/system/param")
public class SysParamController {
    
    @Autowired
    private SysParamService sysParamService;
    
    /**
     * 获取所有可编辑参数
     */
    @GetMapping("/editable")
    public ResponseEntity<List<SysParam>> getAllEditableParams() {
        List<SysParam> params = sysParamService.getAllEditableParams();
        return ResponseEntity.ok(params);
    }
    
    /**
     * 获取所有系统参数
     */
    @GetMapping("/all")
    public ResponseEntity<List<SysParam>> getAllSystemParams() {
        List<SysParam> params = sysParamService.getAllSystemParams();
        return ResponseEntity.ok(params);
    }
    
    /**
     * 根据参数键名获取参数值
     */
    @GetMapping("/value/{paramKey}")
    public ResponseEntity<String> getParamValue(@PathVariable String paramKey) {
        String value = sysParamService.getParamValue(paramKey);
        return ResponseEntity.ok(value);
    }
    
    /**
     * 根据参数键名获取参数对象
     */
    @GetMapping("/detail/{paramKey}")
    public ResponseEntity<SysParam> getParamByKey(@PathVariable String paramKey) {
        SysParam param = sysParamService.getParamByKey(paramKey);
        return ResponseEntity.ok(param);
    }
    
    /**
     * 更新系统参数
     */
    @PutMapping("/update")
    public ResponseEntity<Boolean> updateParam(@RequestBody SysParamVO paramVO) {
        boolean result = sysParamService.updateParam(paramVO);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 批量更新系统参数
     */
    @PutMapping("/batch-update")
    public ResponseEntity<Boolean> batchUpdateParams(@RequestBody List<SysParamVO> paramVOs) {
        boolean result = sysParamService.batchUpdateParams(paramVOs);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 刷新系统参数缓存
     */
    @PostMapping("/refresh-cache")
    public ResponseEntity<Boolean> refreshParamCache() {
        sysParamService.refreshParamCache();
        return ResponseEntity.ok(true);
    }
    
    /**
     * 获取参数映射（键值对）
     */
    @GetMapping("/map")
    public ResponseEntity<Map<String, String>> getParamMap() {
        Map<String, String> paramMap = sysParamService.getParamMap();
        return ResponseEntity.ok(paramMap);
    }
    
    /**
     * 检查参数键名是否存在
     */
    @GetMapping("/check-key")
    public ResponseEntity<Boolean> checkParamKeyExist(
            @RequestParam String paramKey, 
            @RequestParam(required = false) Long id) {
        boolean exists = sysParamService.checkParamKeyExist(paramKey, id);
        return ResponseEntity.ok(exists);
    }
}